package demoMod.invincibleOne.cards.invincible;

import basemod.abstracts.CustomSavable;
import basemod.cardmods.InnateMod;
import basemod.helpers.CardModifierManager;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.vfx.cardManip.ShowCardAndObtainEffect;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.actions.SelectCardInCardGroupAction;
import demoMod.invincibleOne.enums.CardTagsEnum;
import demoMod.invincibleOne.interfaces.IOnAddToMasterDeck;

public class DirtyCursing extends AbstractInvincibleCard implements CustomSavable<String>, IOnAddToMasterDeck {
    public static final String ID = InvincibleOneMod.makeID("DirtyCursing");

    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG_PATH = "cards/DirtyCursing.png";

    private static final CardType TYPE = CardType.SKILL;
    private static final CardRarity RARITY = CardRarity.RARE;
    private static final CardTarget TARGET = CardTarget.NONE;

    private String savedCardId1;
    private String savedCardId2;

    private static final int COST = -2;

    public DirtyCursing() {
        super(ID, NAME, InvincibleOneMod.getResourcePath(IMG_PATH), COST, DESCRIPTION, TYPE, RARITY, TARGET);
        this.baseMagicNumber = this.magicNumber = 1;
        this.tags.add(CardTagsEnum.ALERT);
    }

    @Override
    public void onAddToMasterDeck() {
        InvincibleOneMod.addToBot(new SelectCardInCardGroupAction(this.magicNumber, c -> c.color == CardColor.CURSE || c.hasTag(CardTagsEnum.VIRUS), c -> {
            CardModifierManager.addModifier(c, new InnateMod());
            savedCardId1 = c.cardID;
        }, AbstractDungeon.player.masterDeck, SelectCardInCardGroupAction.TEXT[1]));
    }


    @Override
    public Runnable getUpgradeAction() {
        return () -> {
            upgradeMagicNumber(1);
            if (AbstractDungeon.player != null && AbstractDungeon.player.masterDeck.contains(this)) {
                InvincibleOneMod.addToBot(new SelectCardInCardGroupAction(1, c -> c.color == CardColor.CURSE || c.hasTag(CardTagsEnum.VIRUS),
                        c -> {CardModifierManager.addModifier(c, new InnateMod());
                savedCardId2 = c.cardID;},
                        AbstractDungeon.player.masterDeck, SelectCardInCardGroupAction.TEXT[1]));
            }
        };
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {

    }

    @Override
    public String onSave() {
        return savedCardId1 + "," + savedCardId2;
    }

    @Override
    public void onLoad(String s) {
        if (s != null) {
            String[] sc = s.split(",");
            for (AbstractCard card : AbstractDungeon.player.masterDeck.group) {
                if (card.cardID.equals(sc[0]) || card.cardID.equals(sc[1])) {
                    CardModifierManager.addModifier(card, new InnateMod());
                    break;
                }
            }
            savedCardId1 = sc[0];
            savedCardId2 = sc[1];
        }
    }
}
